package vip.xiaonuo.pay.modular.alipay.controller;

import com.alipay.api.domain.*;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.common.cache.CommonCacheOperator;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.util.CommonGeneratePayOrderNoUtil;
import vip.xiaonuo.pay.modular.alipay.enums.AliPayStateEnums;
import vip.xiaonuo.pay.modular.alipay.service.IAliPayService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 支付宝支付
 */
@Api(tags = "支付宝支付")
@ApiSupport(author = "coffee", order = 1)
@RestController
@RequestMapping("/pay/alipay")
public class AliPayController {

    @Resource
    private IAliPayService alipayService;

    /**
     * app统一收单下单并支付页面接口的调用
     *
     * @param model
     * @return
     */
    @PostMapping("/tradeAppPay")
    public CommonResult tradeAppPay(@RequestBody AlipayTradeAppPayModel model) {
        String formStr = alipayService.tradeAppPay(model);

        // 将form表单字符串返回给前端程序之后，前端将会调用自动提交脚本，进行表单的提交
        // 此时，表单会自动提交到action属性所指向的支付宝开放平台中，从而为用户展示一个支付页面
        return CommonResult.ok(formStr);
    }

    @Resource
    private CommonCacheOperator commonCacheOperator;

    /**
     * 统一收单下单并支付页面接口
     *
     * @param model
     * @return
     */
    @PostMapping("/tradePagePay")
    public CommonResult tradePagePay(@RequestBody AlipayTradePagePayModel model) {
        String formStr = alipayService.tradePagePay(model);
        return CommonResult.ok(formStr);
    }

    /**
     * 统一收单交易退款接口
     *
     * @param model
     * @return
     */
    @PostMapping("/tradeRefund")
    public CommonResult tradeRefund(@RequestBody AlipayTradeRefundModel model) {
        String msg = alipayService.tradeRefund(model);
        return CommonResult.ok(msg);
    }

    /**
     * 统一收单交易查询
     *
     * @param model
     * @return
     */
    @PostMapping("/tradeQuery")
    public CommonResult tradeQuery(@RequestBody AlipayTradeQueryModel model) {
        String msg = alipayService.tradeQuery(model);
        return CommonResult.ok(msg);
    }

    /**
     * 统一关单接口
     *
     * @param model
     * @return
     */
    @PostMapping("/tradeClose")
    public CommonResult tradeClose(@RequestBody AlipayTradeCloseModel model) {
        String msg = alipayService.tradeClose(model);
        return CommonResult.ok(msg);
    }

    /**
     * 统一收单交易退款查询
     *
     * @param model
     * @return
     */
    @PostMapping("/tradeFastPayRefundQuery")
    public CommonResult tradeFastPayRefundQuery(@RequestBody AlipayTradeFastpayRefundQueryModel model) {
        String msg = alipayService.tradeFastPayRefundQuery(model);
        return CommonResult.ok(msg);
    }

    /**
     * 同步通知回调
     *
     * @param request
     * @return
     */
    @PostMapping("/handleReturn")
    public CommonResult handleReturn(HttpServletRequest request) {

        // 调用回调处理方法
        boolean result = alipayService.handleReturn(request);
        return CommonResult.ok(String.valueOf(result));
    }

    /**
     * 异步通知回调
     *
     * @param params
     * @return
     */
    @PostMapping("/handleNotify")
    public String handleNotify(@RequestParam Map<String, String> params) {
        boolean handleNotify = alipayService.handleNotify(params);
        return handleNotify ? AliPayStateEnums.PAY_SUCCESS.getValue() : AliPayStateEnums.PAY_FAIL.getValue();
    }
}
